package com.example.aircraftwar.application;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import com.example.aircraftwar.R;
import com.example.aircraftwar.application.game.EasyGame;
import com.example.aircraftwar.application.game.Game;
import com.example.aircraftwar.application.game.GameActivity;
import com.example.aircraftwar.application.game.HardGame;
import com.example.aircraftwar.application.game.NormalGame;
import com.example.aircraftwar.application.loginorregister.LoginOrRegisterActivity;
import com.example.aircraftwar.scorelist.ScoreRankActivity;
import com.example.aircraftwar.startgame.StartGameActivity;

public class MainActivity extends AppCompatActivity {

    private Display display;
    public static Handler mhandler;
    public static int WINDOW_HEIGHT;
    public static int WINDOW_WIDTH;
    private static Intent intent;
    private GameActivity gameActivity;
    public static Game game;
    private StartGameActivity startGameActivity;
    private ScoreRankActivity scoreRankActivity;
    private LoginOrRegisterActivity loginOrRegisterActivity;
    private static Button singleButton;
    private static Button onlineButton;
    public static boolean isonline;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        singleButton = findViewById(R.id.singleplayerbutton);
        singleButton.setBackgroundResource(R.drawable.button);
        onlineButton = findViewById(R.id.onlinebutton);
        onlineButton.setBackgroundResource(R.drawable.button);

        singleButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    singleButton.setBackgroundResource(R.drawable.buttonclick);
                }else if(motionEvent.getAction() == MotionEvent.ACTION_UP){
                    singleButton.setBackgroundResource(R.drawable.button);
                    isonline = false;
                    intent = new Intent(MainActivity.this, startGameActivity.getClass());
                    startActivity(intent);
                }
                return false;
            }
        });
        onlineButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    onlineButton.setBackgroundResource(R.drawable.buttonclick);
                }else if(motionEvent.getAction() == MotionEvent.ACTION_UP){
                    onlineButton.setBackgroundResource(R.drawable.button);
                    isonline = true;
                    intent = new Intent(MainActivity.this, loginOrRegisterActivity.getClass());
                    startActivity(intent);
                }
                return false;
            }
        });

        mhandler = new Mhandler();
        getScreenHW();
        loginOrRegisterActivity = new LoginOrRegisterActivity();
        startGameActivity = new StartGameActivity();
        scoreRankActivity = new ScoreRankActivity();
        gameActivity = new GameActivity();

    }

    public void getScreenHW() {
        display = getWindowManager().getDefaultDisplay();
        WINDOW_HEIGHT = display.getHeight();
        WINDOW_WIDTH = display.getWidth();
    }

    class Mhandler extends Handler {

        // 通过复写handlerMessage() 从而确定更新UI的操作
        @Override
        public void handleMessage(Message msg) {
            // 根据不同线程发送过来的消息，执行不同的UI操作
            // 根据 Message对象的what属性 标识不同的消息
            switch (msg.what) {
                case 1:
                    if (msg.obj.equals("EasyGame")) {
                        game = new EasyGame(MainActivity.this);
                    } else if (msg.obj.equals("NormalGame")) {
                        game = new NormalGame(MainActivity.this);
                    } else {
                        game = new HardGame(MainActivity.this);
                    }
//                    gameActivity = new GameActivity();
                    intent = new Intent(MainActivity.this, gameActivity.getClass());
                    startActivity(intent);
                    break;
                case 2:
                    gameActivity.finish();
                    intent = new Intent(MainActivity.this, scoreRankActivity.getClass());
                    startActivity(intent);
                    break;
            }
        }
    }

    void exitAlert() {
        AlertDialog.Builder bld = new AlertDialog.Builder(this);
        bld.setMessage("您是否确定退出游戏？");
        bld.setTitle("温馨提示：");
        bld.setPositiveButton("确认", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int swich) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                startActivity(intent);
                System.exit(0);
            }
        });
        bld.setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int swich) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        bld.create().show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //exit();
            exitAlert();
            return false;
        }
        else {
            return super.onKeyDown(keyCode, event);
        }
    }
}